package com.ruoyi.quality.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quality.domain.DefectClass;
import com.ruoyi.quality.domain.bo.DefectClassBo;
import com.ruoyi.quality.domain.vo.DefectClassVo;
import com.ruoyi.quality.mapper.DefectClassMapper;
import com.ruoyi.quality.service.IDefectClassService;
import com.ruoyi.system.mapper.SysDictDataMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 问题分类Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-05
 */
@RequiredArgsConstructor
@Service
public class DefectClassServiceImpl implements IDefectClassService {

    private final DefectClassMapper baseMapper;
    private final SysDictDataMapper dictDataMapper;

    /**
     * 查询问题分类
     */
    @Override
    public DefectClassVo queryById(Integer id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询问题分类列表
     */
    @Override
    public TableDataInfo<DefectClassVo> queryPageList(DefectClassBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DefectClass> lqw = buildQueryWrapper(bo);
        Page<DefectClassVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询问题分类列表
     */
    @Override
    public List<DefectClassVo> queryList(DefectClassBo bo) {
        LambdaQueryWrapper<DefectClass> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询问题分类列表
     */
    @Override
    public List<Object> queryClassList(DefectClassBo bo) {

        LambdaQueryWrapper<DefectClass> lqw = buildQueryWrapper(bo);
        List<DefectClassVo> defectClassVoList = baseMapper.selectVoList(lqw);
        //根据通报名称分组
        Map<String, List<DefectClassVo>> map = defectClassVoList.stream().filter(item-> StringUtils.isNotBlank(item.getDefectClass())).collect(Collectors.groupingBy(DefectClassVo::getDefectClass));
        List<Object> resultlist = new ArrayList<>();
        List<SysDictData> sysDictData = dictDataMapper.selectDictDataByType("defect_class");
        sysDictData.forEach(item->{
            String label = item.getDictLabel();
            String value = item.getDictValue();
            HashMap<String, Object> formatMap = getDefectClassHashMap(map,label,value);
            resultlist.add(formatMap);
        });

        return resultlist;
    }
    private static HashMap<String, Object> getDefectClassHashMap(Map<String, List<DefectClassVo>> map,String label,String order) {
        //正式职工列表
        HashMap<String,Object> result = new HashMap<>(8);
        List<DefectClassVo> orderList = map.get(order);
        if (orderList != null){
            List<Object> list = new ArrayList<>();
            orderList.forEach(item->{
                Map<String,Object> newMap = new HashMap<>(8);
                String classLabel = item.getContent();
                newMap.put("label",classLabel);
                newMap.put("value",classLabel);
                list.add(newMap);
            });
            result.put("children",list);
        }
        result.put("label",label);
        return result;
    }

    private LambdaQueryWrapper<DefectClass> buildQueryWrapper(DefectClassBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DefectClass> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getContent()), DefectClass::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getDefectClass()), DefectClass::getDefectClass, bo.getDefectClass());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), DefectClass::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增问题分类
     */
    @Override
    public Boolean insertByBo(DefectClassBo bo) {
        DefectClass add = BeanUtil.toBean(bo, DefectClass.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改问题分类
     */
    @Override
    public Boolean updateByBo(DefectClassBo bo) {
        DefectClass update = BeanUtil.toBean(bo, DefectClass.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DefectClass entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除问题分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
